<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      [v-cloak] {
        display: none;
      }
      .list-item {
        display: inline-block;
        margin-right: 10px;
      }
      .list-enter-active,
      .list-leave-active {
        transition: all 3s;
      }
      .list-enter,
      .list-leave-to {
        opacity: 0;
        transform: translateY(30px);
      }

    </style>
  </head>
  <body>
    <div id="list-demo" class="demo">
      <button v-on:click="add">添加</button>
      <button v-on:click="remove">移除</button>
      <!-- v-for渲染的列表结构，使用transition-group进行动画 -->
      <!--transition-group 会渲染成dom结构 默认是span  tag属性来修改  -->
      <!-- transition-group 内部的元素 不能是行内元素inline，  -->
      <transition-group tag="div" name="list">
        <span v-for="item in items" v-bind:key="item" class="list-item">
          {{ item }}
        </span>
      </transition-group>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <script>
      var vm = new Vue({
        el: "#list-demo",
        data: {
          items: [1, 2, 3, 4, 5, 6, 7, 8, 9],
          nextNum: 10,
        },
        methods: {
          randomIndex: function () {
            return Math.floor(Math.random() * this.items.length);
          },
          add: function () {
            this.items.splice(this.randomIndex(), 0, this.nextNum++);
          },
          remove: function () {
            this.items.splice(this.randomIndex(), 1);
          },
        },
      });
    </script>
  </body>
</html>